<?php
namespace app\admin\controller;

\think\Loader::import('controller/Controller', \think\Config::get('traits_path') , EXT);

use app\admin\Controller;
use think\Request;
use think\Db;
use think\Validate;

class RoomHotelNumber extends Controller
{
    use \app\admin\traits\controller\Controller;
    // 方法黑名单
    protected static $blacklist = [];

    protected function filter(&$map)
    {
        if ($this->request->param("room_type_id")) {
            $map['room_type_id'] = ["like", "%" . $this->request->param("room_type_id") . "%"];
        }
        if ($this->request->param("number_of_rooms")) {
            $map['number_of_rooms'] = ["like", "%" . $this->request->param("number_of_rooms") . "%"];
        }
        if ($this->request->param("create_time")) {
            $map['create_time'] = ["like", "%" . $this->request->param("create_time") . "%"];
        }
    }

    /**
     * 添加酒店房型
     */
//    public function add(Request $request)
//    {
//        if ($request->isPost()){
//            $postData = $request->post();// 接收数据
//            // 验证数据
//            $rule = [
//                'hotel_id'=>'require',
//                'room_type_id'=>'require',
//                'number_of_rooms'=>'require',
//            ];
//            $message = [
//                'hotel_id.require' => '酒店名称不能为空',
//                'room_type_id.require' => '酒店房型不能为空',
//                'number_of_rooms.require' => '房间数量不能为空',
//            ];
//            $validate = new Validate($rule,$message);
//            $result = $validate->check($postData);
//            if (!$result){
//                return ajax_return_error($validate->getError());
//            }
//            // 保存数据
//            $postData['create_time'] = time();
//            $addIds = Db::name('room_hotel_number')->where('hotel_id',$postData['hotel_id'])
//                      ->where('room_type_id',$postData['room_type_id'])->find();
//            if($addIds){
//                return ajax_return_error('已存在该房型数据');
//            }else{
//                $addId = Db::name('room_hotel_number')->strict(false)->insertGetId($postData);
//                if ($addId) {
//                    return ajax_return_adv('添加成功');
//                } else {
//                    return ajax_return_error('添加失败');
//                }
//            }
//        }else{
//            $data = input();
//            // 查询酒店
//            $hotel = Db::name('hotel')->where('id',$data['hotel_id'])->find();
//            // 查询所有房型
//            $hotel_room_type = Db::name('hotel_room_type')->where('isdelete',0)->select();
//            return view('edit',compact('hotel','vo','hotel_room_type'));
//        }
//    }

    /**
     * 编辑页面
     */
    public function edit(Request $request)
    {
        if ($request->isPost()){
            $postData = $request->post(); // 接收数据
            // 验证数据
            $rule = [
                'hotel_id'=>'require',
                'room_type_id'=>'require',
                'number_of_rooms'=>'require',
            ];
            $message = [
                'hotel_id.require' => '酒店名称不能为空',
                'room_type_id.require' => '酒店房型不能为空',
                'number_of_rooms.require' => '房间数量不能为空',
            ];
            $validate = new Validate($rule,$message);
            $result = $validate->check($postData);
            if (!$result){
                return ajax_return_error($validate->getError());
            }
            // 保存数据
            $addId = Db::name('room_hotel_number')->strict(false)->update($postData);
            if ($addId){
                return ajax_return_adv('添加成功');
            }else{
                return ajax_return_error('添加失败');
            }
        }else{
            $data = input();
            // 查询酒店信息
            $vo = Db::name('room_hotel_number')->where('id',$data['id'])->find();
            if (!$vo){
                return ajax_return_error('不存在该酒店房型信息');
            }
            // 查询酒店
            $hotel = Db::name('hotel')->where('id',$vo['hotel_id'])->find();
            // 查询所有房型
            $hotel_room_type = Db::name('hotel_room_type')->where('isdelete',0)->select();
            return view('edit',compact('hotel','vo','hotel_room_type'));
        }
    }

    /**
     * 首页
     */
    public function index(Request $request)
    {

        if ($request->isPost()){

            $postData = $request->post();// 接收数据
            // 验证数据
            $rule = [
                'hotel_id'=>'require',
                'room_type_id'=>'require',
                'number_of_rooms'=>'require',
            ];
            $message = [
                'hotel_id.require' => '酒店名称不能为空',
                'room_type_id.require' => '酒店房型不能为空',
                'number_of_rooms.require' => '房间数量不能为空',
            ];
            $validate = new Validate($rule,$message);
            $result = $validate->check($postData);
            if (!$result){
                return ajax_return_error($validate->getError());
            }
            // 保存数据
            $postData['create_time'] = time();
            $addIds = Db::name('room_hotel_number')->where('hotel_id',$postData['hotel_id'])
                ->where('room_type_id',$postData['room_type_id'])->find();
            if($addIds){
                return ajax_return_error('已存在该房型数据');
            }else{
                $addId = Db::name('room_hotel_number')->strict(false)->insertGetId($postData);
                if ($addId) {
                    return ajax_return_adv('添加成功');
                } else {
                    return ajax_return_error('添加失败');
                }
            }
        }else{
            $data = input();
            if (!empty($data['hotel_ids'])){
                $hotel_id = $data['hotel_ids'];
            }
            if (empty($data['hotel_id'])){
                $map['hotel_id'] = $data['id'];
                $hotel_id = $data['id'];
            }else{
                $hotel_id = $data['hotel_id'];
                $map['hotel_id'] = $data['hotel_id'];
            }
            if ($data['room_type_id']){ // 酒店房型
                $map['room_type_id'] = $data['room_type_id'];
            }

            if ($data['number_of_rooms']) {//酒店数量
                $map['number_of_rooms'] = ['like',"%{$data['number_of_rooms']}%"];
            }
            if ($data['create_time']){
                $start_time = "{$data['create_time']} 0:0:0"; //开始时间
                $end_time = "{$data['create_time']} 23:59:59"; // 结束时间
                $map['create_time'] = ['between',[strtotime($start_time),strtotime($end_time)]];
            }
            if ($this::$isdelete){
                $map['isdelete'] = 1;
            }else{
                $map['isdelete'] = 0;
            }

            // 根据id查找
            $lists = Db::name('room_hotel_number')->where($map)->select();
            foreach ($lists as $index => $item) {
                $lists[$index]['hotel_id'] = Db::name('hotel')->where('id',$item['hotel_id'])->value('hotel_name');
                $lists[$index]['room_type_id'] = Db::name('hotel_room_type')->where('id',$item['room_type_id'])->value('room_name');
            }
            $hotel_room_type = Db::name('hotel_room_type')->where('isdelete',0)->select();
            $list = Db::name('room_hotel_number')->where($map)->order('id','desc')->paginate();
            $count = $list->total();
            $this->view->assign("page", $list->render());
            return view('',compact('list','count','hotel_room_type','hotel_id'));
        }

    }
    
    /**
     * 编辑页面
     * @description 保存编辑房型的数量
     */
    public function save(Request $request)
    {
        if ($request->isPost()){
            $postData = $request->post();
            // 验证数据
            if(!$postData['id']){
                return ajax_return_adv_error('缺少id');
            }
            if(!$postData['num']){
                return ajax_return_adv_error('缺少房间数量');
            }
            // 查询房型的数量
            $find = Db::name('room_hotel_number')->where('id',$postData['id'])->find();
            if (!$find){
                return ajax_return_adv_error('不存在该房型信息');
            }elseif ($find['number_of_rooms'] == $postData['num']){
                return ajax_return_adv('更新成功');
            }
            $up = Db::name('room_hotel_number')->where('id',$postData['id'])->update(['number_of_rooms'=>$postData['num']]);
            if ($up){
                return ajax_return_adv('更新成功');
            }
        }else{
            return ajax_return_adv_error('非法请求');
        }
    }
}
